package CCRMClient.GUI;

import javax.swing.JPanel;

import java.awt.Color;

import javax.swing.border.LineBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;

import java.awt.SystemColor;

import javax.swing.JTextField;
import javax.swing.JButton;

import CCRMClient.ClientStart;
import CCRMClient.Controllers.TemplateManager;
import CCRMServer.DBConnect;
import Common.SERVER_COMMANDS;
import Common.Entities.MarketingTemplate;








import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;

/**
 * This class is a screen of searching a marketing template
 * @author AL
 *
 */
public class TemplateSearchScreen extends JPanel 
{
	
	private JLabel lblSearchTemplate;
	private JLabel lblTempID;
	private JTextField textTempID;
	private JLabel lblTempName;
	private JTextField textTempName;
	private JLabel lblTempDesc;
	private JTextField textTempDesc;
	private JLabel lblProdID;
	private JTextField textProdID;
	private JLabel lblMarketingMessage;
	private JTextArea textAreaMsg;
	private JLabel lblTempStatus;
	private JTextField textTempStatus;
	private JButton btnBackAction;
	private JButton btnSearchAction;
	private JButton btnUpdateAction;

	

	/**
	 * Create the panel.
	 */
	public TemplateSearchScreen() 
	{
	
		initialize();
	}
	
	public TemplateSearchScreen(MarketingTemplate tmp) 
	{
		initialize();
		textTempID.setText(Integer.toString(tmp.getTemplateID()));
		textTempName.setText(tmp.getTemplateName());
		textTempDesc.setText(tmp.getTemplateDescription());
		textAreaMsg.setText(tmp.getTemplateMessage());
		textTempStatus.setText(tmp.getTemplateStatus());
		textProdID.setText(Integer.toString(tmp.getProductID()));
		btnSearchAction.setEnabled(false);
	}
	
	private void initialize() 
	{
		setSize(450,372);
		setBorder(new LineBorder(new Color(0, 0, 0)));
		setBackground(Color.WHITE);
		setLayout(null);
		
		add(getLblAddNewTemplate());
		
		add(getLblTempID());
		add(getTextTempID());
		
		add(getLblTempName());
		add(getTextTempName());
		
		add(getLblTempDesc());
		add(getTextTempDesc());
		
		add(getLblProdID());
		add(getTextProdID());
		
		add(getLblMsg());
		add(getTextAreaMsg());
		
		add(getLblTempStatus());
		add(getTextTempStatus());
		
		add(getBtnSearchAction());
		add(getBtnBackAction());
	}

		
	JLabel getLblAddNewTemplate()
	{
		if (lblSearchTemplate == null) 
		{
			lblSearchTemplate = new JLabel("Search Marketing Template");
			lblSearchTemplate.setHorizontalAlignment(SwingConstants.CENTER);
			lblSearchTemplate.setForeground(Color.RED);
			lblSearchTemplate.setFont(new Font("David", Font.BOLD, 26));
			lblSearchTemplate.setBackground(Color.WHITE);
			lblSearchTemplate.setBounds(0, 0, 450, 49);
		}
		return lblSearchTemplate;
	}
	
	
	JLabel getLblTempID() 
	{
		if (lblTempID == null) 
		{
	     lblTempID = new JLabel("Template ID:");
	     lblTempID.setFont(new Font("David", Font.BOLD, 20));
	     lblTempID.setBounds(10, 50, 130, 29);
		}
		return lblTempID;
	}
	
	
	JTextField getTextTempID() 
	{
		if (textTempID == null) 
		{
	     textTempID = new JTextField();
	     textTempID.setColumns(10);
	     textTempID.setBackground(SystemColor.menu);
	     textTempID.setBounds(171, 55, 251, 20);
		}
		return textTempID;
	}
	
	
	JLabel getLblTempName() 
	{
		if (lblTempName == null) 
		{
			lblTempName = new JLabel("Template Name:");
			lblTempName.setFont(new Font("David", Font.BOLD, 20));
			lblTempName.setBounds(10, 112, 164, 29);
		}
		return lblTempName;
	}
	
	
	JTextField getTextTempName() 
	{
		if (textTempName == null) 
		{
			textTempName = new JTextField();
			textTempName.setEditable(false);
			textTempName.setBackground(SystemColor.control);
			textTempName.setBounds(171, 117, 251, 20);
			textTempName.setColumns(10);
		}
		return textTempName;
	}
	

	
	JLabel getLblTempDesc() 
	{
		if (lblTempDesc == null) 
		{
			lblTempDesc = new JLabel("Description:");
			lblTempDesc.setFont(new Font("David", Font.BOLD, 20));
			lblTempDesc.setBounds(10, 143, 115, 29);
		}
		return lblTempDesc;
	}
	

	JTextField getTextTempDesc() 
	{
		if (textTempDesc == null) 
		{
	       textTempDesc = new JTextField();
	       textTempDesc.setEditable(false);
	       textTempDesc.setColumns(10);
	       textTempDesc.setBackground(SystemColor.menu);
	       textTempDesc.setBounds(171, 148, 251, 20);
		}
		return textTempDesc;
	}
	
	JLabel getLblMsg() 
	{
		if ( lblMarketingMessage == null) 
		{
	      lblMarketingMessage = new JLabel("Message:");
	      lblMarketingMessage.setFont(new Font("David", Font.BOLD, 20));
	      lblMarketingMessage.setBounds(10, 210, 164, 29);
		}
		return  lblMarketingMessage;
	}
	
	
	JTextArea getTextAreaMsg() 
	{
		if (textAreaMsg == null) 
		{
			textAreaMsg = new JTextArea();
			textAreaMsg.setEditable(false);
			textAreaMsg.setBorder(new LineBorder(new Color(0, 0, 0)));
			textAreaMsg.setBackground(SystemColor.control);
			textAreaMsg.setFont(new Font("Tahoma", Font.PLAIN, 11));
			textAreaMsg.setLineWrap(true);
			textAreaMsg.setBounds(171, 210, 251, 73);
		}
		return textAreaMsg;
	}
	
	
	
	private JLabel getLblProdID() 
	{
		if (lblProdID == null) 
		{
			lblProdID = new JLabel("Product ID:");
			lblProdID.setFont(new Font("David", Font.BOLD, 20));
			lblProdID.setBounds(10, 174, 115, 29);
		}
		return lblProdID;
	}
	
	private JTextField getTextProdID() 
	{
		if (textProdID == null) {
			textProdID = new JTextField();
			textProdID.setEditable(false);
			textProdID.setColumns(10);
			textProdID.setBackground(SystemColor.menu);
			textProdID.setBounds(171, 179, 251, 20);
		}
		return textProdID;
		
	}
	
	JLabel getLblTempStatus() 
	{
		if (lblTempStatus == null) 
		{
			lblTempStatus = new JLabel("Template Status:");
			lblTempStatus.setFont(new Font("David", Font.BOLD, 20));
			lblTempStatus.setBounds(10, 82, 153, 29);
		}
		return lblTempStatus;
	}
	
	JTextField getTextTempStatus() 
	{
		if (textTempStatus == null) 
		{
			textTempStatus = new JTextField();
			textTempStatus.setEditable(false);
			textTempStatus.setColumns(10);
			textTempStatus.setBackground(SystemColor.menu);
			textTempStatus.setBounds(171, 86, 251, 20);
		}
		return textTempStatus;
	}
		
	JButton getBtnBackAction() {
		if (btnBackAction == null) {
			btnBackAction = new JButton("Back");
			btnBackAction.addActionListener(new BtnBackActionActionListener());
			btnBackAction.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnBackAction.setBounds(140, 294, 83, 23);
		}
		return btnBackAction;
	}

	JButton getBtnSearchAction() {
		if (btnSearchAction == null) {
			btnSearchAction = new JButton("Search");
			btnSearchAction.addActionListener(new BtnSearchActionActionListener());
			btnSearchAction.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnSearchAction.setBounds(233, 294, 83, 23);
		}
		return btnSearchAction;
	}
	/**
	 * This inner class implements event listener for pressing Back Action button
	 * @author AL
	 *
	 */
	private class BtnBackActionActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			textTempID.setText("");
			textTempName.setText("");
			textTempDesc.setText("");
			textProdID.setText("");
			textAreaMsg.setText("");
			TemplateManagementScreen tmScreen = new TemplateManagementScreen();
			setVisible(false);
			ClientStart.panelSwicher(tmScreen);
		}
	}
	
	/**
	 * This inner class implements event listener for pressing Search Action button
	 * @author AL
	 *
	 */
	private class BtnSearchActionActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			String tempName;
			String tempID;
			String tempDesc;
			String ProdID;
			String tempMsg;
			String tempStatus;
			try{
				tempID = textTempID.getText();
			}catch (NullPointerException e){
					ClientStart.appWindow.notifyUser(null, "Action Failed", "INFORMATION_MESSAGE");
				}
		    	tempID = textTempID.getText();
				tempName = null;
				tempDesc = null;
				ProdID = "0";
				tempMsg = null;
				tempStatus = null;
				try {
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.SEARCH_TEMPLATE, TemplateManager.SearchTemplate(tempID,tempName,tempDesc, tempMsg, tempStatus, ProdID));
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
					ClientStart.quitApp();
				}
			}
		}
}